# pip install onnx==1.14.1 onnxruntime==1.16.0 # 使用opset==19

import mxnet as mx
import numpy as np
from mxnet.contrib import onnx as onnx_mxnet


import logging
logging.basicConfig(level=logging.INFO)

# Download pre-trained resnet model - json and params by running following code.
# 这里也可以自己合并链接到浏览器手动下载 | 默认会下载到当前目录
#path='http://data.mxnet.io/models/imagenet/'
#[mx.test_utils.download(path+'resnet/18-layers/resnet-18-0000.params'),
# mx.test_utils.download(path+'resnet/18-layers/resnet-18-symbol.json'),
# mx.test_utils.download(path+'synset.txt')]


dir = './mobilenet-v2-model/params/'
# Downloaded input symbol and params files
sym = dir+'./1.0000-imagenet-mobilenetv2_1.0-symbol.json'
params = dir+'./1.0000-imagenet-mobilenetv2_1.0-0000.params'

# Standard Imagenet input - 3 channels, 224*224
input_shape = (1,3,224,224)

# Path of the output file
onnx_file = './mobilenet-v2-model/mxnet_exported_imagenet-mobilenetv2_1.0.onnx'

# 模型转换已经封装很好了，一行命令即可
converted_model_path = onnx_mxnet.export_model(sym, params, [input_shape], np.float32, onnx_file)


from onnx import checker
import onnx


# Check validity of ONNX model 检查导出 onnx 的可用性
# Load onnx model
model_proto = onnx.load_model(converted_model_path)

# Check if converted ONNX protobuf is valid
checker.check_graph(model_proto.graph)
